//
//  BHStadiumServiceManager.swift
//  BigHealth
//
//  Created by 布丁 on 2021/8/2.
//

import UIKit

class BHStadiumServiceManager: NSObject {
    var stadiumId: String = ""
    // 场馆详情的回调
    var stadiumDetailMainResponseBlock:((BHStadiumDetailModel) -> Void)?
    /**商品列表的回调*/
    var stadiumDetailProductResponseBlock:(([BHStadiumDetailGoodModel]) -> Void)?
    // 教练的回调
    var stadiumDetailCoachResponseBlock:(([BHCoacheModel]) -> Void)?
    // 赛事的回调
    var stadiumDetailGameResponseBlock:(([BHStadiumDetailGameModel]) -> Void)?
    // 活动的回调
    var stadiumDetailPlayResponseBlock:(([BHStadiumDetailPlayModel]) -> Void)?

    
    // 所有请求都走了
    var stadiumResponseCompleteHandler:(() -> Void)?
    
    
    // MARK: 场馆列表的参数
    var pageNo: Int = 1
    var cityName: String = ""
    var regionName: String = ""
    var type: Int = 1  // 1场馆 2医疗机构
    var searchKey: String = "" // 搜索字段
    var typeOneValue: String = ""
    var typeTwoValue: String = ""
    var sort: String = "" //1：智能排序 2：离我最近 3：购买最多 4：价格最低
    var responseForMainStadiumListBlock:((Bool,String,Int,[BHCommonStadiumModel]) -> Void)?
    
    
    
    
    func refreshStadiumService()  {
        SVProgressHUD.show()

        /**详情*/
        let group = DispatchGroup.init()
        group.enter()
        let detailManager = BHHTTPClient.init()
        detailManager.get(url: String.stadium_detail_Url, paramters: ["id":self.stadiumId]) { (success, errorMsg, responseObject)  in
            
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseObject, key: "result")
                let model: BHStadiumDetailModel = BHStadiumDetailModel.init()
                model.parseDic(result)
                self.stadiumDetailMainResponseBlock?(model)
            }else {
                BHToast.toast(text: errorMsg)
            }
            
            group.leave()
        }
        
        /**商品列表*/
        group.enter()
        let producetManager = BHHTTPClient.init()
        producetManager.get(url: String.stadium_detail_product_listUrl, paramters: ["id":self.stadiumId]) { (success, errorMsg, responseObject)  in
            var temp: [BHStadiumDetailGoodModel] = []
            if success == true {
                print("商品列表：\(responseObject)")
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for dic in result {
                    let model: BHStadiumDetailGoodModel = BHStadiumDetailGoodModel.init()
                    model.parseDic(dic as! [String : Any])
                    temp.append(model)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.stadiumDetailProductResponseBlock?(temp)

            group.leave()
        }
        
        /**教练列表*/
        group.enter()
        let coachManager = BHHTTPClient.init()
        coachManager.get(url: String.stadium_detail_coach_listUrl, paramters: ["id":self.stadiumId]) { (success, errorMsg, responseObject)  in
            
            var temp: [BHCoacheModel] = []
             if success == true {
                print("教练列表：\(responseObject)")
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for dic in result {
                    let model: BHCoacheModel = BHCoacheModel.init()
                    model.parseDic(dic as! [String : Any])
                    temp.append(model)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.stadiumDetailCoachResponseBlock?(temp)
            group.leave()
        }
        
        
        /**赛事列表*/
        group.enter()
        let gameManager = BHHTTPClient.init()
        gameManager.get(url: String.stadium_detail_game_listUrl, paramters: ["id":self.stadiumId]) { (success, errorMsg, responseObject)  in
            var temp: [BHStadiumDetailGameModel] = []
            if success == true {
                print("赛事列表：\(responseObject)")
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for dic in result {
                    let model: BHStadiumDetailGameModel = BHStadiumDetailGameModel.init()
                    model.parseDic(dic as! [String : Any])
                    temp.append(model)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.stadiumDetailGameResponseBlock?(temp)

            group.leave()
        }
        
        /**赛事列表*/
        group.enter()
        let playManager = BHHTTPClient.init()
        playManager.get(url: String.stadium_detail_play_listUrl, paramters: ["id":self.stadiumId]) { (success, errorMsg, responseObject)  in
            var temp: [BHStadiumDetailPlayModel] = []
            if success == true {
                print("活动列表：\(responseObject)")
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for dic in result {
                    let model: BHStadiumDetailPlayModel = BHStadiumDetailPlayModel.init()
                    model.parseDic(dic as! [String : Any])
                    temp.append(model)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.stadiumDetailPlayResponseBlock?(temp)

            group.leave()
        }
        
        
        
        group.notify(queue: DispatchQueue.main) {
            SVProgressHUD.dismiss()
            
            self.stadiumResponseCompleteHandler?()
        }
    }
    
}


// 赛事列表的接口
extension BHStadiumServiceManager {
    // 刚进入
    func queryStadiumListAllService()  {
        let dic = self.appendDic()
        let manager = BHHTTPClient.init()
        manager.get(url: String.stadium_main_listUrl, paramters: dic) { (success, errorMsg, responseDic) in
            var tempArray: [BHCommonStadiumModel] = []
            var total: Int = 0
            
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                total = BHJsonTool.decodeIntFromDic(dic: result, key: "total")

                let record = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
                for dic in record {
                    let model = BHCommonStadiumModel.init()
                    model.parseDic(dic as! [String : Any])
                    tempArray.append(model)
                }
            }
            
            self.responseForMainStadiumListBlock?(success,errorMsg,total,tempArray)
        }
        
    }
    
   
    private func appendDic() -> [String:Any] {
        var dic: [String:Any] = [:]
        dic["pageNo"] = self.pageNo
        dic["pageSize"] = 20
        dic["cityName"] = self.cityName
        dic["regionName"] = self.regionName
        dic["type"] = self.type
        dic["longitude"] = BHLocationManager.shared.longitude
        dic["latitude"] = BHLocationManager.shared.latitude
        dic["search"] = self.searchKey
        dic["typeOneValue"] = self.typeOneValue
        dic["typeTwoValue"] = self.typeTwoValue
        dic["sort"] = self.sort
        return dic
    }
}
